$(function () {
  // 获取用户信息
  UserInfo();
  function UserInfo() {
    $.ajax({
      url: 'http://localhost:8080/api/v1/admin/user/detail',
      headers: {
        Authorization: localStorage.getItem('token')
      },
      success: function (res) {

        // 更新编辑页用户信息
        $('#username').val(res.data.username);
        $('#nickname').val(res.data.nickname);
        $('#email').val(res.data.email);
        $('.user_pic').prop('src', res.data.userPic);
        $('#password').val(res.data.password);


        // 更新主页用户信息
        $('.user_info span', window.parent.document).text(res.data.nickname);
        $('.user_info img', window.parent.document).prop('src', res.data.userPic);
        $('.user_center_link img', window.parent.document).prop('src', res.data.userPic);

      }
    });
  }


  // 编辑用户信息
  $('.btn-edit').on('click', function (e) {
    e.preventDefault();
    const formdata = new FormData(document.querySelector('#form'));
    $.ajax({
      url: 'http://localhost:8080/api/v1/admin/user/edit',
      type: 'post',
      data: formdata,
      processData: false,
      contentType: false,
      headers: {
        Authorization: localStorage.getItem('token')
      },
      success: function (res) {
        layer.msg('更新信息成功', { time: 1000 }, function () {
          UserInfo();

        });
      }
    })
  });

  // 图片上传显示新图片
  $('#exampleInputFile').on('change', function () {
    let file = this.files[0];
    let src = URL.createObjectURL(file);
    $('.user_pic').prop('src', src);
  });
})